﻿using System.Runtime.Caching;

namespace Koala.Pro.Common
{
    public static class MemoryCacheHelper
    {
        private static readonly object locker = new object();

        public static T GetCacheItem<T>(string key, Func<T> cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Invalid cache key");
            }
            if (cachePopulate == null)
            {
                throw new ArgumentNullException("cachePopulate");
            }
            if (!slidingExpiration.HasValue && !absoluteExpiration.HasValue)
            {
                throw new ArgumentException("Either a sliding expiration or absolute must be provided");
            }
            if (MemoryCache.Default[key] == null)
            {
                lock (locker)
                {
                    if (MemoryCache.Default[key] == null)
                    {
                        CacheItem item = new CacheItem(key, cachePopulate());
                        CacheItemPolicy policy = CreatePolicy(slidingExpiration, absoluteExpiration);
                        MemoryCache.Default.Add(item, policy);
                    }
                }
            }
            return (T)MemoryCache.Default[key];
        }

        private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration)
        {
            CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
            if (absoluteExpiration.HasValue)
            {
                cacheItemPolicy.AbsoluteExpiration = absoluteExpiration.Value;
            }
            else if (slidingExpiration.HasValue)
            {
                cacheItemPolicy.SlidingExpiration = slidingExpiration.Value;
            }
            cacheItemPolicy.Priority = CacheItemPriority.Default;
            return cacheItemPolicy;
        }

        public static void ClearCache()
        {
            foreach (string item in MemoryCache.Default.Select<KeyValuePair<string, object>, string>((KeyValuePair<string, object> kvp) => kvp.Key).ToList())
            {
                MemoryCache.Default.Remove(item);
            }
        }

        public static void AddItem(string key, object value)
        {
            if (MemoryCache.Default.Contains(key))
            {
                return;
            }
            lock (locker)
            {
                if (!MemoryCache.Default.Contains(key))
                {
                    MemoryCache.Default.Add(key, value, DateTimeOffset.MaxValue);
                }
            }
        }

        public static void RemoveItem(string key)
        {
            if (!MemoryCache.Default.Contains(key))
            {
                return;
            }
            lock (locker)
            {
                if (MemoryCache.Default.Contains(key))
                {
                    MemoryCache.Default.Remove(key);
                }
            }
        }

        public static bool ContainItem(string key)
        {
            return MemoryCache.Default.Contains(key);
        }

        public static object GetItem(string key)
        {
            object obj = MemoryCache.Default.Get(key);
            if (obj != null)
            {
                return obj;
            }
            lock (locker)
            {
                if (MemoryCache.Default.Contains(key))
                {
                    obj = MemoryCache.Default[key];
                }
                return obj;
            }
        }

        public static T GetItem<T>(string key)
        {
            T val = (T)MemoryCache.Default.Get(key);
            if (val != null)
            {
                return val;
            }
            lock (locker)
            {
                if (MemoryCache.Default.Contains(key))
                {
                    val = (T)MemoryCache.Default[key];
                }
                return val;
            }
        }
    }

}
